<template>
    <div>
      <el-card>
        <div class="header-container">
          <span class="header-title">医生管理</span>
          <el-button type="primary" @click="showAddDialog">添加医生</el-button>
        </div>
  
        <el-table :data="doctors" style="width: 100%" border>
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="姓名" prop="name"></el-table-column>
          <el-table-column label="科室" prop="department"></el-table-column>
          <el-table-column label="地址" prop="address"></el-table-column>
          <el-table-column label="电话" prop="phone"></el-table-column>
          <el-table-column label="年龄" prop="age"></el-table-column>
          <el-table-column label="性别" prop="sex"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <!-- 添加医生的对话框 -->
        <el-dialog :visible.sync="dialogVisible" title="添加医生">
          <el-form :model="newDoctor" label-width="80px">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="newDoctor.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="newDoctor.password"></el-input>
            </el-form-item>
            <el-form-item label="科室">
              <el-select v-model="newDoctor.department" placeholder="请选择科室">
                <el-option v-for="department in departments" :key="department.id" :label="department.name" :value="department.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="newDoctor.address"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="newDoctor.phone"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model="newDoctor.age"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-input v-model="newDoctor.sex"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="addDoctor">确认添加</el-button>
          </div>
        </el-dialog>
  
        <el-dialog :visible.sync="editDialogVisible" title="编辑医生">
  <el-form :model="editingDoctor" label-width="80px">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="editingDoctor.name"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="editingDoctor.password"></el-input>
    </el-form-item>
    <el-form-item label="科室">
      <el-select v-model="editingDoctor.department" placeholder="请选择科室">
        <el-option v-for="department in departments" :key="department.id" :label="department.name" :value="department.name"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="editingDoctor.address"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="editingDoctor.phone"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="editingDoctor.age"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="editingDoctor.sex"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取消</el-button>
    <el-button type="primary" @click="updateDoctor">确认编辑</el-button>
  </div>
</el-dialog>

      </el-card>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        doctors: [],
        newDoctor: {
          name: '',
          password: '',
          department: '',
          address: '',
          phone: '',
          age: '',
          sex: '',
        },
        dialogVisible: false,
        editDialogVisible: false, // 添加编辑对话框的显示控制变量
        editingDoctor: {}, // 保存正在编辑的医生数据
      };
    },
    methods: {
      showAddDialog() {
        this.dialogVisible = true;
      },
      addDoctor() {
        if (!this.newDoctor.name || !this.newDoctor.password || !this.newDoctor.department || !this.newDoctor.address || !this.newDoctor.phone || !this.newDoctor.age || !this.newDoctor.sex) return this.$message.error('信息不全');
  
    axios
          .post('http://127.0.0.1:3000/api/doctors', this.newDoctor)
          .then((response) => {
            this.getDoctors();
            this.dialogVisible = false;
            this.$message.success('医生添加成功');
          })
          .catch((error) => {
            console.error('添加医生时发生错误：', error);
            this.$message.error('添加医生失败');
          });
  
        
      },
      handleEdit(doctor) {
        // 设置编辑对话框为可见
        this.editDialogVisible = true;
        // 保存正在编辑的医生数据
        this.editingDoctor = { ...doctor };
      },
      handleDelete(doctorId) {
        axios
          .delete(`http://127.0.0.1:3000/api/doctors/${doctorId}`)
          .then((response) => {
            this.getDoctors();
            this.$message.success('医生删除成功');
          })
          .catch((error) => {
            console.error('删除医生时发生错误：', error);
            this.$message.error('删除医生失败');
          });
      },
      getDoctors() {
        axios
          .get('http://127.0.0.1:3000/api/doctors')
          .then((response) => {
            this.doctors = response.data;
          })
          .catch((error) => {
            console.error('获取医生列表时发生错误：', error);
            this.$message.error('获取医生列表失败');
          });
      },
      fetchDepartments() {
        axios
          .get('http://127.0.0.1:3000/api/departments')
          .then((response) => {
            this.departments = response.data;
          })
          .catch((error) => {
            console.error('获取科室列表时发生错误：', error);
            this.$message.error('获取科室列表失败');
          });
      },
      updateDoctor() {
        
    // Send a request to update the doctor data on the backend
    axios
      .put(`http://127.0.0.1:3000/api/doctors/${this.editingDoctor.id}`, this.editingDoctor)
      .then((response) => {
        this.getDoctors();
        this.editDialogVisible = false;
        this.$message.success('医生信息更新成功');
      })
      .catch((error) => {
        console.error('更新医生信息时发生错误：', error);
        this.$message.error('医生信息更新失败');
      });
  },
    },
    mounted() {
      this.getDoctors();
      this.fetchDepartments();
    },
  };
  </script>
  
  <style scoped>
  .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
  }
  
  .header-title {
    font-size: 18px;
    font-weight: bold;
  }
  
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
  </style>
  